'use strict';//管理员登录
const Controller = require('egg').Controller;
const utility = require("utility")
class LoginController extends Controller {
    async index() {
        const { ctx, app } = this;
        const { user_name, password } = ctx.request.body;
        let user;
        let pwd
        if (user_name && password) {
            user = await app.mysql.query('select user_name from admin_name');
            pwd = await app.mysql.query('select password from admin_name');
            const result = user.find(item => item.user_name === user_name);
            const _pwd = utility.md5(password)
            const results = pwd.find(item => item.password === _pwd);

            if (result && results) {
                const iner = {
                    user_name: 'token',
                    role: '管理员',
                    login_time: Date.now() / 1000,
                    end_time: Date.now() / 1000 + 3600 * 2
                }
                const token = app.jwt.sign(iner, app.config.jwt.secret)
                ctx.body = {
                    code: 0,
                    msg: '登陆成功',
                    token
                }
            } else {
                ctx.body = {
                    code: 404,
                    msg: '账号或密码错误'
                }

            }
        } else {
            ctx.body = {
                code: 400,
                msg: '格式不正确'
            }
        }
    }
}
module.exports = LoginController;
